//
//  MainViewController.m
//  MyPopStar
//
//  Created by tarena on 16/1/5.
//  Copyright © 2016年 ssnb. All rights reserved.
//

#define btnWidthPer 0.534
#define btnHightPer 0.0703125
#define btnSpacingPer   0.046875
#define halfBtnSpacingPer   btnSpacingPer/2

#import "MainViewController.h"
#import "GameController.h"
#import "StarCalculation.h"
#import "FireView.h"
@interface MainViewController ()

@property (nonatomic,strong) UIImageView *backgroundView;
@property (nonatomic,strong) UIButton *btnNewGame;//新游戏
@property (nonatomic,strong) UIButton *btnResume;//恢复游戏
@property (nonatomic,strong) UIButton *btnRate;//排行榜

@end

@implementation MainViewController
#pragma mark --初始化
- (void)viewDidLoad {
    [super viewDidLoad];
    [self mInit];
}

-(void)mInit{
    [self backgroundView];
    [self popStart];
    [self bgAnimation];
    [self btnRate];
}
-(void)popStart{

    UIImageView *pop = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"pop"]];
    UIImageView *star = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"star"]];
    pop.contentMode = UIViewContentModeScaleAspectFit;
    star.contentMode = UIViewContentModeScaleAspectFit;
    [self.view addSubview:star];
    [self.view addSubview:pop];
    WeakSelf(content);
    [star mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(content.view.mas_centerY).mas_equalTo(-0.03125*viewHeight);
        make.width.mas_equalTo(viewWidth*0.583);
        make.height.mas_equalTo(viewHeight*0.21875);
        make.left.mas_equalTo(content.view.mas_centerX).mas_equalTo(-0.125*viewWidth);
    }];
    [pop mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(viewWidth*0.639);
        make.height.mas_equalTo(viewHeight*0.21875);
        make.bottom.mas_equalTo(star.mas_top).mas_equalTo(50);
        make.right.mas_equalTo(content.view.mas_centerX).mas_equalTo(0.18*viewWidth);
        
    }];
    
}

-(void)bgAnimation{
    CAEmitterLayerView *fireView = [[FireView alloc] initWithFrame:CGRectMake(0, 0, viewWidth, viewHeight*0.7)];
    fireView.fireImgName=@"fire";
    [fireView show];
    [self.view addSubview:fireView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
-(BOOL)prefersStatusBarHidden{
    return YES;
}

mdealloc

#pragma mark --按钮监听

-(void)startNewGame:(UIButton *)btn{
    
    GameController *gameController = [[GameController alloc]init];
    [self presentViewController:gameController animated:YES completion:nil];
    
    
}
-(void)resumeGame:(UIButton *)btn{
    MyLog(@"resumGame");
    GameController *gameController = [[GameController alloc]initWithResume];
    [self presentViewController:gameController animated:YES completion:nil];
}

-(void)rate:(UIButton *)btn{
}

#pragma  mark -- 截图代码
-(void)pic{
    NSString *str = [[NSBundle mainBundle] pathForResource:@"character" ofType:@"plist"];
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:str];
    UIImage *img = [UIImage imageNamed:@"character"];
    for (NSString *picname in dic) {
        NSDictionary *diction =[dic objectForKey:picname];
        CGRect rect = CGRectFromString([diction objectForKey:@"fram"]);
        [self getSubImage:img mCGRect:rect newImgName:picname];
    }

}

/**
 *  @author ssnb, 16-01-05 21:01:06
 *
 *  用于截取原始图片中的某块区域图片内容并保存到Documents下
 *
 *  @param image   原始图片
 *  @param mCGRect 要截取的位置
 *  @param name    截取后文件的名字
 *
 *  @return 截取后返回截取到的图像
 */
-(UIImage*)getSubImage:(UIImage *)image mCGRect:(CGRect)mCGRect newImgName:(NSString *)name;
{
    
    CGImageRef subImageRef = CGImageCreateWithImageInRect(image.CGImage, mCGRect);
    CGRect smallBounds = CGRectMake(0, 0, CGImageGetWidth(subImageRef), CGImageGetHeight(subImageRef));
    
    UIGraphicsBeginImageContext(smallBounds.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawImage(context, smallBounds, subImageRef);
    UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];
    UIGraphicsEndImageContext();
    
    NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    NSString *filePath =[documentPath stringByAppendingPathComponent:name];
    
    NSData *data = UIImagePNGRepresentation(smallImage);
    [[NSFileManager defaultManager] createFileAtPath:filePath contents:data attributes:nil];
    return smallImage;
}
#pragma  mark -- 懒加载
//背景图
- (UIImageView *)backgroundView {
    if(_backgroundView == nil) {
        _backgroundView = [[UIImageView alloc] init];
        _backgroundView.image = [UIImage imageNamed:@"bg_main"];
        [self.view addSubview:_backgroundView];
        WeakSelf(content);
        [_backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(content.view);
        }];
    }
    return _backgroundView;
}
//开始游戏按钮
- (UIButton *)btnNewGame {
	if(_btnNewGame == nil) {
		_btnNewGame = [[UIButton alloc] init];
        [_btnNewGame setImage:[UIImage imageNamed:@"newgame"] forState:(UIControlStateNormal)];
        [_btnNewGame addTarget:self action:@selector(startNewGame:) forControlEvents:(UIControlEventTouchUpInside)];
        [self.view addSubview:_btnNewGame];
        WeakSelf(content);
        [_btnNewGame mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(0);
            make.top.mas_equalTo(content.view.mas_centerY).mas_equalTo(halfBtnSpacingPer*viewHeight);
            make.width.mas_equalTo(btnWidthPer*viewWidth);
            make.height.mas_equalTo(btnHightPer*viewHeight);
        }];
	}
	return _btnNewGame;
}
//继续游戏按钮
- (UIButton *)btnResume {
	if(_btnResume == nil) {
		_btnResume = [[UIButton alloc] init];
        [_btnResume setImage:[UIImage imageNamed:@"resume"] forState:(UIControlStateNormal)];
        [_btnResume addTarget:self action:@selector(resumeGame:) forControlEvents:(UIControlEventTouchUpInside)];
        [self.view addSubview:_btnResume];
        WeakSelf(content);
        [_btnResume mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(content.btnNewGame);
            make.centerX.mas_equalTo(0);
            make.top.mas_equalTo(content.btnNewGame.mas_bottom).mas_equalTo(btnSpacingPer*viewHeight);
            
        }];
	}
	return _btnResume;
}
//排名按钮
- (UIButton *)btnRate {
	if(_btnRate == nil) {
		_btnRate = [[UIButton alloc] init];
        [_btnRate setImage:[UIImage imageNamed:@"rate"] forState:(UIControlStateNormal)];
        [_btnRate addTarget:self action:@selector(rate:) forControlEvents:(UIControlEventTouchUpInside)];
        [self.view addSubview:_btnRate];
        WeakSelf(content);
        [_btnRate mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(content.btnNewGame);
            make.centerX.mas_equalTo(0);
            make.top.mas_equalTo(content.btnResume.mas_bottom).mas_equalTo(btnSpacingPer*viewHeight);
        }];
	}
	return _btnRate;
}


@end
